package com.xizimall.search.service.impl;

import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xizimall.search.dao.SearchDao;
import com.xizimall.search.pojo.SearchResult;
import com.xizimall.search.service.SearchService;
/**
 * 搜索service
 * <p>Title: SearchServiceImpl</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2018年4月24日下午8:57:53
 * @version 1.0
 */
@Service
public class SearchServiceImpl implements SearchService {
    
	@Autowired
	private SearchDao searchDao;
	
	@Override
	public SearchResult search(String queryString, int page, int rows) throws Exception {
		//创建查询条件
		SolrQuery query=new SolrQuery();
		//设置查询条件
		query.setQuery(queryString);
		query.setStart((page-1)*rows);
		query.setRows(rows);
		//设置默认搜索域
		query.set("df", "item_keywords");
		//设置高亮显示
		query.setHighlight(true);
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<em style=\"color:red\">");
		query.setHighlightSimplePost("</em>");
		//执行查询
		SearchResult searchResult=searchDao.search(query);
		//计算查询结果总页数
		long recordCount=searchResult.getRecordCount();
		long pageCount=recordCount/rows;
		if(recordCount%rows>0){
			pageCount++;
		}
		searchResult.setPageCount(pageCount);
		searchResult.setCurPage(page);
		return searchResult;
	}

}
